#include "eventwrap.h"
/*
static void on_tcperrorcb(evlistenptr listener, void* arg)
{
    ioevtcp_t* tcp = (ioevtcp_t*)(arg);
    c_error(tcp != NULL);
    c_error(tcp->event != NULL);
    tcp->event(tcp, IOEVTCP_ERROR);
}
static void on_tcpacceptcb(evlistenptr listen, _sock_t fd, struct sockaddr* addr, int sl, void* arg)
{
    ioevtcp_t* tcp = (ioevtcp_t*)(arg);
    c_error(tcp != NULL);
    c_error(tcp->accept != NULL);
    tcp->accept(tcp, addr, sl, fd);
    logi("on_tcpacceptcb");
}
static void on_readcb(struct bufferevent* bev, void* arg)
{
    ioevstream_t* stream = (ioevstream_t*)(arg);
    c_error(stream != NULL);
    c_error(stream->read != NULL);

}
static void on_writecb(struct bufferevent* bev, void* arg)
{
    ioevstream_t* stream = (ioevstream_t*)(arg);
    c_error(stream != NULL);
    c_error(stream->write != NULL);
}
static void on_eventcb(struct bufferevent* bev, short what, void* arg)
{
    ioevstream_t* stream = (ioevstream_t*)(arg);
    c_error(stream != NULL);
    c_error(stream->event != NULL);
}
*/

static int stream_connsock(ioevstream_t* stream, SOCKADDR_PTR sa, socklen_t sl, TVL_PTR tv)
{
    rc_error(stream != NULL, S_ERROR);
    return S_SUCCESS;
}
int ioevloop_setnotify(ioevloop_t* loop, evloop_notify cb)
{
    rc_error(loop != NULL, S_ERROR);
    loop->notify = cb;
    return S_SUCCESS;
}
int ioevloop_setctx(ioevloop_t* loop, void* ctx)
{
    rc_error(loop != NULL, S_ERROR);
    loop->ctx = ctx;
    return S_SUCCESS;
}
void* ioevloop_getctx(ioevloop_t* loop)
{
    rc_error(loop != NULL, NULL);
    return loop->ctx;
}
int ioevtcp_init(ioevtcp_t* tcp, ioevloop_t* evloop, _sock_t sock)
{
    rc_error(tcp != NULL, S_ERROR);

    return S_SUCCESS;
}
int ioevtcp_uninit(ioevtcp_t* tcp)
{
    rc_error(tcp != NULL, S_ERROR);
    return S_SUCCESS;
}
int ioevtcp_setctx(ioevtcp_t* tcp, void* ctx)
{
    rc_error(tcp != NULL, S_ERROR);
    tcp->ctx = ctx;
    return S_SUCCESS;
}
void* ioevtcp_getctx(ioevtcp_t* tcp)
{
    rc_error(tcp != NULL, NULL);
    return tcp->ctx;
}
int ioevtcp_seteventcb(ioevtcp_t* tcp, tcp_eventcb cb)
{
    rc_error(tcp != NULL, S_ERROR);
    tcp->event = cb;
    return S_SUCCESS;
}
int ioevtcp_setacceptcb(ioevtcp_t* tcp, tcp_acceptcb cb)
{
    rc_error(tcp != NULL, S_ERROR);
    tcp->accept = cb;
    return S_SUCCESS;
}
int ioevtcp_bind(ioevtcp_t* tcp, NET_ADDR ip, NET_ADDR_PORT port)
{
    rc_error(tcp != NULL, S_ERROR);

    return S_SUCCESS;
}
int ioevtcp_bind6(ioevtcp_t* tcp, NET_ADDR ip, NET_ADDR_PORT port)
{
    rc_error(tcp != NULL, S_ERROR);
    return S_SUCCESS;
}
int ioevtcp_enable(ioevtcp_t* tcp)
{
    rc_error(tcp != NULL, S_ERROR);

    return S_SUCCESS;
}
int ioevtcp_disable(ioevtcp_t* tcp)
{
    rc_error(tcp != NULL, S_ERROR);

    return S_SUCCESS;
}

int ioevstream_init(ioevstream_t* stream, ioevloop_t* evloop, _sock_t sock)
{
    rc_error(stream != NULL, S_ERROR);
    rc_error(evloop != NULL, S_ERROR);

    return S_SUCCESS;
}
int ioevstream_uninit(ioevstream_t* stream)
{
    rc_error(stream != NULL, S_ERROR);

    return S_SUCCESS;
}
int ioevstream_assign(ioevstream_t* stream, _sock_t sock)
{
    rc_error(stream != NULL, S_ERROR);

    return S_SUCCESS;
}
int ioevstream_setctx(ioevstream_t* stream, void* ctx)
{
    rc_error(stream != NULL, S_ERROR);
    stream->ctx = ctx;
    return S_SUCCESS;
}
void* ioevstream_getctx(ioevstream_t* stream)
{
    rc_error(stream != NULL, NULL);
    return stream->ctx;
}
int ioevstream_seteventcb(ioevstream_t* stream, stream_eventcb cb)
{
    rc_error(stream != NULL, S_ERROR);
    stream->event = cb;
    return S_SUCCESS;
}
int ioevstream_setreadcb(ioevstream_t* stream, stream_readcb cb)
{
    rc_error(stream != NULL, S_ERROR);
    stream->read = cb;
    return S_SUCCESS;
}
int ioevstream_setwritecb(ioevstream_t* stream, stream_writecb cb)
{
    rc_error(stream != NULL, S_ERROR);
    stream->write = cb;
    return S_SUCCESS;
}
int ioevstream_write(ioevstream_t* stream, buf_ptr ptr, buf_len len, int sec)
{
    struct timeval timeout;

    rc_error(stream != NULL, S_ERROR);

    timeout.tv_sec = sec;
    timeout.tv_usec = 0;

    return S_SUCCESS;
}
int ioevstream_read(ioevstream_t* stream, buf_ptr ptr, buf_len len, int sec)
{
    struct timeval tv;

    rc_error(stream != NULL, S_ERROR);

    tv.tv_sec = sec;
    tv.tv_usec = 0;

    return S_SUCCESS;
}
int ioevstream_connectsock(ioevstream_t* stream, NET_ADDR ip, NET_ADDR_PORT port, int sec)
{
    struct sockaddr_in saddr;
    struct timeval tv;

    rc_error(stream != NULL, S_ERROR);

    tv.tv_sec = sec;
    tv.tv_usec = 0;

    s_memset(&saddr, 0, sizeof(struct sockaddr_in));
    saddr.sin_family = AF_INET;
    saddr.sin_addr.s_addr = inet_addr(ip);
    saddr.sin_port = htons(port);

    stream_connsock(stream, (struct sockaddr*)&saddr, sizeof(saddr), &tv);

    return S_SUCCESS;
}
int ioevstream_connect6sock(ioevstream_t* stream, NET_ADDR ip, NET_ADDR_PORT port, int sec)
{
    struct sockaddr_in6 saddr6;
    struct timeval tv;

    rc_error(stream != NULL, S_ERROR);

    tv.tv_sec = sec;
    tv.tv_usec = 0;

    s_memset(&saddr6, 0, sizeof(struct sockaddr_in6));

    saddr6.sin6_family = AF_INET6;
    _inet_pton(AF_INET6, ip, (char*)&saddr6.sin6_addr);
    saddr6.sin6_port = htons(port);

    stream_connsock(stream, (struct sockaddr*)&saddr6, sizeof(saddr6), &tv);

    return S_SUCCESS;
}
